﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MLOCore;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows.Forms;

namespace MusicLibraryOrganizer
{
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window, INotifyPropertyChanged
  {

    ObservableCollection<Library> Libraries { get; set; }

    private Library _library;
    public Library CurrentLibrary
    {
      get
      {
        if (_library == null) _library = new Library();
        return _library;
      }
    }

    public Window1()
    {
      InitializeComponent();

      //this.txtDir.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
      //this.txtDir.Text = "C:\\Users\\Public\\Music\\Sample Music";

      this.DataContext = CurrentLibrary;
      Libraries = new ObservableCollection<Library>();
      SourcesListBox.ItemsSource = Libraries;
      
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
    //  CurrentLibrary.LoadFromDirectory(this.txtDir.Text);

      OnPropertyChanged("CurrentLibrary");
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    /// <summary>
    /// Triggers the PropertyChanged event.
    /// </summary>
    public virtual void OnPropertyChanged(string propertyName)
    {
      if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion

    private void AddLibrary_Click(object sender, RoutedEventArgs e)
    {
      newLibraryBorder.Visibility = Visibility.Visible;

    }

    private void CreateNewLibraryBtn_Click(object sender, RoutedEventArgs e)
    {
      if (String.IsNullOrEmpty(newLibraryNameTxtBox.Text) || !Directory.Exists(newLibraryPathTxtBox.Text))
      {
        //TODO:  Eliminate the shitty popup window that handles validation feedback to the user.
        System.Windows.MessageBox.Show("Please enter a valid name and Root Path.");
        return;
      }

      Library l = new Library();
      l.Name = newLibraryNameTxtBox.Text;
      l.ParentDirectory = newLibraryPathTxtBox.Text;

      Libraries.Add(l);

      newLibraryBorder.Visibility = Visibility.Collapsed;

    }

    private void BrowseBtn_Click(object sender, RoutedEventArgs e)
    {
      FolderBrowserDialog folderDlg = new FolderBrowserDialog();
      folderDlg.ShowDialog();

      if (folderDlg.SelectedPath != null)
      {
        newLibraryPathTxtBox.Text = folderDlg.SelectedPath;
      }
    }

    public void OnAnalyzeLibrary(object sender, ExecutedRoutedEventArgs e)
    {
      string libraryName = e.Parameter as string;

      if (!string.IsNullOrEmpty(libraryName))
      {
        Library lib = Libraries.Single<Library>(l => l.Name == libraryName);

        lib.Analyze();
      }


    }
  }
  public class MyCommands
  {
    public static RoutedCommand AnalyzeLibrary = new RoutedCommand("AnalyzeLibrary", typeof(Window1));

  }
}
